<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['customerCode', validatorRules.customerCode]" @change="customerChange" placeholder="请选择客户">
                <a-select-option v-for="item in customer" :value="item.customerCode">
                  {{ item.customerCode }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['customerName']" placeholder="请输入客户名称"  disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuCode']" placeholder="请输入品番编码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品番名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['skuName']" placeholder="请输入品番名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="背番" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['backNo']" placeholder="请输入背番"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="英文名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['english']" placeholder="请输入英文名"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['brand']" placeholder="请输入品牌"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="产地" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['producer']" placeholder="请输入产地"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否有效" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['valid']" placeholder="请选择状态" style="width: 100%">
                <a-select-option :value=1>有效</a-select-option>
                <a-select-option :value=0>无效</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="长(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['length']" placeholder="请输入长"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="宽(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['width']" placeholder="请输入宽"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="高(mm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['high']" placeholder="请输入高"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单个重量(g)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['weight']" placeholder="请输入单个重量（g）"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BasicSkuDetailForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/basicSkuDetail/basicSkuDetail/add",
          edit: "/basicSkuDetail/basicSkuDetail/edit",
          queryById: "/basicSkuDetail/basicSkuDetail/queryById",
          getShelfType: '/basicShelfType/basicShelfType/getAll',
          getCustomer: '/basicCustomer/basicCustomer/getAll'
        },
        shelfType: [],
        customer: [],
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.getShelfType();
      this.getCustomer();
    },
    methods: {
      getCustomer(){
        getAction(this.url.getCustomer).then((res)=>{
          if(res.success){
            this.$nextTick(() => {
              this.customer = res.result;
              console.log(this.customer);
            })
          }
        })
      },
      customerChange(customerCode){
        console.log(customerCode);
        this.customer.forEach(element => {
          if(customerCode == element.customerCode){
            this.form.setFieldsValue({
              customerName: element.name
            })
          }
        });
      },
      getShelfType(){
        getAction(this.url.getShelfType).then((res)=>{
          if(res.success){
            this.$nextTick(() => {
              this.shelfType = res.result;
              console.log(this.shelfType);
            })
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          // this.form.setFieldsValue(pick(this.model,'skuCode','skuName','skuCustomer','picUrl','length','width','high','weight','usefulLife'))
          this.form.setFieldsValue(pick(this.model,'customerCode','customerName','skuCode','skuName','backNo','english','brand','producer','valid','length','width','high','weight'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        // this.form.setFieldsValue(pick(row,'skuCode','skuName','skuCustomer','picUrl','length','width','high','weight','usefulLife'))
        this.form.setFieldsValue(pick(row,'customerCode','customerName','skuCode','skuName','backNo','english','brand','producer','valid','length','width','high','weight'))
      },
    }
  }
</script>